package com.mysimplenews.news.model;

import android.content.Context;
import android.os.SystemClock;

import com.duowan.mobile.netroid.DefaultRetryPolicy;
import com.duowan.mobile.netroid.Listener;
import com.duowan.mobile.netroid.NetroidError;
import com.duowan.mobile.netroid.NetroidLog;
import com.duowan.mobile.netroid.Request;
import com.duowan.mobile.netroid.request.StringRequest;
import com.mysimplenews.bean.ConfigXml;
import com.mysimplenews.bean.HomeApp;
import com.mysimplenews.bean.Lang;
import com.mysimplenews.http.Netroid;
import com.mysimplenews.news.NewsTabDao;
import com.mysimplenews.provider.TabsProvider;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by cxy on 2016/6/14.
 */
public class NewsModelImp implements NewsModel{

    private NewsTabDao newsTabDao;
    private Context mContext;
    private TabsProvider tabsProvider;

    public NewsModelImp(Context context){
        this.mContext = context;
        tabsProvider = new TabsProvider();
    }

    @Override
    public List<String> loadTabData(final OnLoadTabDataListListener onLoadTabDataListListener) {
        String url = "http://develop.hospitalityinspired.com.cn:8080/basicinfo/theme/5/config.xml";
        StringRequest request = new StringRequest(Request.Method.GET, url, new Listener<String>() {
            long startTimeMs;
            int retryCount;
            @Override
            public void onPreExecute() {
                startTimeMs = SystemClock.elapsedRealtime();
            }

            @Override
            public void onSuccess(String response) {
                onLoadTabDataListListener.onNetSuccess(tabsProvider.getTabs(response));
            }

            @Override
            public void onError(NetroidError error) {
                onLoadTabDataListListener.onFailure(error.getMessage(),error);
            }

            @Override
            public void onCacheSuccess(String response) {
                super.onCacheSuccess(response);
                onLoadTabDataListListener.onCacheSuccess(tabsProvider.getTabs(response));
            }

            @Override
            public void onRetry() {
                long executedTime = SystemClock.elapsedRealtime() - startTimeMs;
                if (++retryCount > 5 || executedTime > 30000) {
                    NetroidLog.e("retryCount : " + retryCount + " executedTime : " + executedTime);
                    Netroid.newRequestQueue().cancelAll("GET");
                }
            }

            @Override
            public void onNetworking() {
                super.onNetworking();
            }
        });
        //不检索缓存情况，马上执行网络请求操作，获得响应结果后放入硬盘缓存中
        //设置超时时长以及重试次数
        request.setRetryPolicy(new DefaultRetryPolicy(5000, 3, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        request.setTag("GET");
        //需要缓存数据设置此方法
        request.setCacheExpireTime(TimeUnit.SECONDS, 10);
//		request.setForceUpdate(true);
        Netroid.newRequestQueue().add(request);

        return null;
    }


    public interface OnLoadTabDataListListener {
        void onCacheSuccess(List<String> list);
        void onNetSuccess(List<String> list);
        void onFailure(String msg, Exception e);
    }


}
